import AntModal from "@/components/pt/dialog/AntModal";
import {
  addBizPushRule,
  updateBizPushRule,
  findMaxSort,
  getBizPushRule,
} from "@/api/biz/bizPushRule";
export default {
  name: "CreateForm",
  props: {
    laiyuanOptions: {
      type: Array,
      required: true,
    },
    cityOptions: {
      type: Array,
      required: true,
    },
  },
  components: {
    AntModal,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "按人员分配",
      // 表单参数
      form: {},
      rules: {
        ruleCities: [
          { required: true, message: "城市不能为空", trigger: "blur" },
        ],
        laiyuan: [{ required: true, message: "线索不能为空", trigger: "blur" }],
      },
      // 来源穿梭框数据源
      laiyuanData: [],
      targetLaiyuanKeys: [],
      // 人员穿梭框数据源
      userData: [],
      targetUserKeys: [],
      // 列表的数据源
      tableData: [],
      defaultPrice: undefined,
      columns: [
        {
          title: "来源",
          dataIndex: "laiyuan",
          ellipsis: true,
          align: "center",
          width: "100px",
        },

        {
          title: "是否启用",
          dataIndex: "isEnable",
          ellipsis: true,
          align: "center",
          width: "80px",
          // fixed: "left",
          scopedSlots: { customRender: "isEnable" },
        },
        {
          title: "日接单量",
          dataIndex: "maxCount",
          ellipsis: true,
          align: "center",
          width: "90px",
          // fixed: "left",
          scopedSlots: { customRender: "maxCount" },
        },
        // {
        //   title: "当日实际接单量",
        //   dataIndex: "sharedCount",
        //   ellipsis: true,
        //   align: "center",
        //   width: "90px",
        //   visible: false,
        // },
        // {
        //   title: "累计接单量",
        //   dataIndex: "totalCount",
        //   ellipsis: true,
        //   align: "center",
        //   width: "90px",
        //   visible: false,
        // },
        // {
        //   title: "权重",
        //   dataIndex: "weight",
        //   ellipsis: true,
        //   align: "center",
        //   width: "90px",
        //   visible: false,
        //   // fixed: "left",
        //   // scopedSlots: { customRender: "weight" },
        // },
      ],
    };
  },
  filters: {},
  created() {},
  computed: {
    currentDeptId() {
      return this.$store.state.user.dept;
    },
  },
  watch: {},
  mounted() {},
  methods: {
    // 城市单选
    onCityChange(value) {
      this.tableData = this.tableData.map((item) => ({
        ...item,
        // 将城市更新
        city: value,
      }));
    },
    // 来源
    handleLaiyuanAdd(value) {
      // this.tableData = [];
      const options = this.form.laiyuan;
      for (let i = 0; i < options.length; i++) {
        if (
          this.tableData.filter((item) => item.laiyuan === options[i])
            .length === 0
        ) {
          this.tableData.push({
            userId: this.form.userId,
            userName: this.form.userName,
            deptId: this.form.deptId,
            deptName: this.form.deptName,
            city: this.form.ruleCities,
            laiyuan: options[i],
            isEnable: true,
            maxCount: 20,
          });
        }
      }
    },
    handleLaiyuanRemove(value) {
      this.tableData = this.tableData.filter((item) => item.laiyuan !== value);
    },
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        amount: undefined,

        laiyuan: undefined,

        sort: undefined,

        status: 0,

        remark: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      row.laiyuan = row.laiyuan === null ? [] : row.laiyuan;

      this.form.userId = row.userId;
      this.form.deptId = row.deptId;
      this.form.deptName = row.deptName;
      this.form.ruleSet = row.ruleSet;
      this.form.ruleLogic = row.ruleLogic;
      this.form.ruleType = row.ruleType;
      this.form.ruleName = row.ruleName;
      this.form.userName = row.ruleName;
      this.form.id = row.id;
      this.form.status = "0";
      this.open = true;
      this.formTitle = "添加[" + row.ruleName + "]分配规则";
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizPushRuleId = row.id;
      getBizPushRule(bizPushRuleId).then((response) => {
        this.form = response.data;
        this.form.ruleUsers = this.form.ruleUsers.map((user) => ({
          ...user,
          // 将字符串 "1"/"0" 转换为布尔值
          isEnable: user.isEnable === "1", // "1" → true，其他值 → false
          city: this.form.ruleCities,
        }));

        this.tableData = this.form.ruleUsers;
        this.form.laiyuan = JSON.parse(this.form.laiyuan);

        this.formTitle = "修改[" + row.ruleName + "]分配规则";

        this.spinning = !this.spinning;
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.ruleUsers = this.tableData.map((item) => ({
            ...item,
            isEnable: item.isEnable === true ? "1" : "0",
          }));

          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (saveForm.laiyuan) {
            saveForm.laiyuan = JSON.stringify(saveForm.laiyuan);
          } else {
            saveForm.laiyuan = null;
          }

          if (this.form.id !== this.form.userId) {
            updateBizPushRule(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizPushRule(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/pushrule/index";
      this.$router.push(index);
    },
  },
};
